package com.banmoon.state;

import lombok.Getter;
import lombok.Setter;

import java.util.Random;

@Getter
@Setter
public class Person {

    private AbstractState state;

    public Person() {
        state = new FullHpState(this);
    }

    public void takeMedicine(){
        state.takeMedicine();
    }

    public void resurrect(){
        state.resurrect();
    }

    public void adventure(){
        state.adventure();
    }

    public static void adventure(Person person){
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            System.out.println("正在探险中~~~");
            int hp = person.getState().getHp() - random.nextInt(40);
            if(hp<=0){
                System.out.println("已阵亡，停止探险");
                person.setState(new NoHpState(person));
                break;
            }
            if(hp<100){
                AbstractState state = person.getState();
                if(state instanceof FullHpState){
                    person.setState(new HalfHpState(person, hp));
                }else if(state instanceof HalfHpState){
                    ((HalfHpState) state).updateHp(hp);
                }
            }
        }
    }

}
